<?php
require_once('version.inc');
require_once('dbaccess.inc');
require_once('language.inc');
require_once('header.inc');
require_once('crypt.inc');
require_once('settings.inc');
require_once('helper.inc');

$drm = array(true, false, false, false, true, true, true, true);
$headline = 10;

require_once('menu.inc');
require_once('knumber.inc');

$func = $_REQUEST['func'];

# In diesem Modul global verwendete Variablen
$sumparr = array(0,0,0,0,0,0,0,0,0);	// Spaltensummen der Eingabezeilen
$sumiarr = array(0,0,0,0,0,0,0,0,0);
$sumallp = 0.0;		// Spaltensumme der Tasks
$sumalli = 0.0;

# Folgende Funktion gibt die IST-Daten bzw. Eingabezeilen fuer einen
# Mitarbeiter innerhalb der Tabelle aus.
#
function ShowMitarbeiter($db, $minum, $anz, $von, $bis, $pr_num, $pl_num, $mi=true) {
	global $pmlight;
	global $rstufe;
	global $leneinheit;
	global $periode;
	global $ist_erledigung;
	global $ist_future;
	global $sumparr;
	global $sumiarr;
	global $sumallp;
	global $sumalli;

	# Ermitteln ab wann noch erfasst werden darf.
	$query = "select ab_datum from abschluss where ab_prnum = $pr_num ";
	$query .= "and ab_plnum = $pl_num order by ab_datum desc";

	if (!($result = QueryDB($db, $query)))
	   return false;

	if (numrowsDB($result) > 0) {
	   $data = fetchDB($result, 0);
	   $ab_datum = $data[0];
	} else
	   $ab_datum = 0;

	# Projekt oder Konto?
	$query = "select pr_status from project where pr_num = $pr_num";

	if (!($result = QueryDB($db, $query)))
	   return false;

	$data = fetchDB($result, 0);
	$pr_status = $data[0];

	# Auslesen der Tasks welche bebucht werden koennen.
	$akt = $von;

	if ($pr_status == 0) {
	   $query = "select distinct on (ta_hash) ta_name, sum(al_hours), ta_num, ta_start, ";
	   $query .= "ta_duration, ta_hash from task, plan, allocation where ";
	   $query .= "pl_num = ta_plnum and al_task = ta_num and al_ressource = $minum and ";
	   $query .= "ta_meeting = 0 and pl_status in (0,2,3,4,5) and ta_level > 1 and ";
	   $query .= "pl_prnum = $pr_num and (ta_plnum = $pl_num or ";
	   $query .= "ta_plnum in (select ta_plnum from wdone, task where ";
	   $query .= "ta_num = wd_task and wd_minum = $minum and wd_prnum = $pr_num and ";
	   $query .= "wd_datum between $von and $bis order by ta_plnum desc)) and ";
//	   $query .= "pl_prnum = $pr_num and ";

	   if (!$ist_future) {
	      $query .= "ta_start <= $bis ";
	   } else {
	      $query .= "(ta_start between $von and $bis or ";
	      $query .= "ta_start+ta_duration*86400 between $von and $bis or ";
	      $query .= "$von between ta_start and ta_start+ta_duration*86400 or ";
	      $query .= "$bis between ta_start and ta_start+ta_duration*86400 or ";
	      $query .= "ta_num in (select wd_task from wdone where ";
	      $query .= "wd_minum = $minum and wd_prnum = $pr_num and ";
	      $query .= "wd_datum between $von and $bis)) ";
	   }

	   $query .= "group by ta_hash, ta_num, ta_start, ta_duration, ta_name order by ta_hash, ta_name";
	} else {
	   $query = "select ta_name, ta_id, ta_num, ta_start, ta_duration, ta_hash from task ";
	   $query .= "where ta_plnum = $pl_num";
	}

	if (!($res = QueryDB($db, $query)))
	   return false;

	$rows = numrowsDB($res);
	$r = 0;

	while ($r < $rows) {
	   $data = fetchDB($res, $r);
	   $ta_name = $data[0];
	   $plan = $data[1];
	   $ta_num = $data[2];
	   $ta_start = $data[3];
	   $ta_duration = $data[4];
	   $ta_hash = $data[5];

	   # Ermitteln des Planstatus zum aktuellen Task. Ist dieser
	   # "Inaktiv" dann darf nur dann eine Planausgabe erfolgen, wenn
	   # es einen IST-Wert in der entsprechenden Periode gibt. Das
	   # betrifft nur die Periodenerfassung ($pmlight == true).
	   if ($pmlight) {
	      $query = "select pl_status from plan, task where ";
	      $query .= "pl_num = ta_plnum and ta_num = $ta_num";

	      if (!($resta = QueryDB($db, $query)))
	         return false;

	      $data = fetchDB($resta, 0);
	      $pl_status = $data[0];
	   }

	   if ($rstufe == 3)
	      $csp = "colspan=2";
	   else
	      $csp = "";

	   echo "<tr><td class=\"sel\" $csp>$ta_name</td>";

	   if ($rstufe != 3 && $mi) {
	      $query = "select mi_nname, mi_vname from mitarbeiter where ";
	      $query .= "mi_num = $minum";

	      if (!($resmi = QueryDB($db, $query)))
	         return false;

	      $data = fetchDB($resmi, 0);

	      if (!$data) {
	         Error("Mitarbeiter konnte nicht ermittelt werden!");
	         return false;
	      }

	      $mi_nname = $data[0];
	      $mi_vname = $data[1];
	      echo "<td class=\"sel\">$mi_nname $mi_vname</td>\n";
	   } else if ($rstufe != 3) {
	      echo "<td class=\"sel\"></td>\n";
	   }

	   # Erledigungsgrad ermitteln
	   $query = "select sum(wd_hours)/$leneinheit from wdone, task ";
	   $query .= "where wd_minum = $minum and wd_prnum = $pr_num and ";
	   $query .= "ta_num = wd_task and ta_hash ='$ta_hash'";

	   if (!($reser = QueryDB($db, $query)))
	      return false;

	   $data = fetchDB($reser, 0);

	   if (!$data) {
	      Error("Erledigungsgrad konnte nicht ermittelt werden!");
	      return false;
	   }

	   $ist = $data[0];

	   if ($ist_erledigung) {
	      if ($plan != 0)
	         $proz = $ist / $plan * 100.0;
	      else
	         $proz = 0.0;
	   }

	   if ($pmlight)
	      echo "<td class=\"sel\">Plan:<br>IST:</td>\n";

	   $sumi = 0.0;
	   $sump = 0.0;
	   $z = 0;
	   $akt = $von;

	   while ($z < $anz) {
	      # Eingabezeilen schreiben (nur wenn Edit erlaubt ist!)
	      if ($pmlight) {
	         $mon = gmdate("n", $akt);
	         $year = gmdate("Y", $akt);
	         $dstart = gmmktime(0, 0, 0, $mon, 1, $year);
	         $dend = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);
	         $query = "select sum(wd_hours)/$leneinheit, wd_edit, wd_task from wdone, task ";
	         $query .= "where wd_minum = $minum and ta_num = wd_task and ";
//	         $query .= "where wd_minum = $minum and ";
	         $query .= "ta_hash = '$ta_hash' and wd_prnum = $pr_num and ";
	         $query .= "wd_datum >= $dstart and wd_datum <= $dend ";
	         $query .= "group by wd_task, wd_edit";
	      } else {
	         $query = "select wd_hours, wd_edit, wd_task from wdone, task where ";
	         $query .= "wd_minum = $minum and wd_prnum = $pr_num and ";
	         $query .= "ta_num = wd_task and ta_hash = '$ta_hash' and ";
	         $query .= "wd_datum between $akt and $akt";
	      }

	      if (!($reswd = QueryDB($db, $query)))
	         return false;

	      if (numrowsDB($reswd) > 0) {
	         $data = fetchDB($reswd, 0);
	         $wd_hours = $data[0];
	         $wd_edit = $data[1];
	         $wd_task = $data[2];
	      } else {
	         $wd_hours = 0;
	         $wd_edit = 1;
	         $wd_task = $ta_num;
	      }

	      if ($pmlight) {
	         if ($pr_status == 0) {
		    $tmon = gmdate("n", $ta_start);
		    $tyear = gmdate("Y", $ta_start);
		    $td = gmmktime(0, 0, 0, $tmon, 1, $tyear);
		 } else
		    $td = 0;

	         if ($ist_future) {
	            if ($pr_status == 0) {
		       $ta_end = $ta_start + $ta_duration * 86400;
		       $temon = gmdate("n", $ta_end);
		       $teyear = gmdate("Y", $ta_end);
		       $te = gmmktime(0, 0, 0, $temon, daysinmonth($temon, $teyear), $teyear);
		       $dcomp = ($akt >= $td && $akt <= $te) ? true : false;
		    } else {
		       $te = 0;
		       $dcomp = true;
		    }
	         } else
	            $dcomp = ($akt >= $td) ? true : false;

	         $query = "select al_hours from allocation where ";
	         $query .= "al_task = $ta_num and al_pstart between $dstart and $dend and ";
	         $query .= "al_ressource = $minum";

	         if (!($resal = QueryDB($db, $query)))
	            return false;

		 if (numrowsDB($resal) > 0) {
		    if ($pl_status != 5 || $wd_hours > 0.0) {
	               $data = fetchDB($resal, 0);
	               $al_hours = $data[0];
		    } else
		       $al_hours = 0.0;

	            $sump += $al_hours;
	            $sumparr[$z] += $al_hours;
	         } else
	            $al_hours = 0;
	      } else if ($ist_future)
	         $dcomp = ($akt >= $ta_start && $akt <= ($ta_start+$ta_duration*86400)) ? true : false;
	      else
	         $dcomp = ($akt >= $ta_start) ? true : false;

	      # Heraus finden, ob der aktuelle Task dem neuesten Plan gehoert,
	      # oder nicht. Wenn es sich um einen aelteren Plan handelt
	      # (Status = Inaktiv) dann darf keine IST-Eingabe moeglich sein.
	      if ($dcomp) {
	         $query = "select pl_status from plan, task where ";
	         $query .= "pl_num = ta_plnum and ta_num = $ta_num";

	         if (!($resdc = QueryDB($db, $query)))
	            return false;

		 $data = fetchDB($resdc, 0);
		 $akt_status = $data[0];

		 if ($akt_status > 1) {
		    $query = "select pl_status from project, plan where ";
		    $query .= "pl_prnum = pr_num and pr_num = $pr_num and ";
		    $query .= "pl_status in (2,3,4,5) ";
		    $query .= "order by pl_lfd desc";

		    if (!($resdc = QueryDB($db, $query)))
		       return false;

		    $data = fetchDB($resdc, 0);
		    $new_status = $data[0];

		    if ($new_status < $akt_status) {
		       $dcomp = false;
		       $pplan = true;
		    }
		 }
	      } else
	         $pplan = false;

	      if ($akt > $ab_datum && $dcomp && ($wd_edit == 't' || $wd_edit == true || $wd_edit == 1)) {
		 if (!$pmlight && (gmdate("w", $akt) == 6 || gmdate("w", $akt) == 0))
		    $class = "seldkgray";
		 else
		    $class = "sel";

	         $name = "dt_" . $minum . "_" . $pr_num . "_" . $wd_task . "_" . $akt;

	         if ($pmlight)
	            printf("<td class=\"selnum\">%s<br><input type=\"text\" name=\"$name\" value=\"%s\" size=6 maxlength=6></td>\n",
			FormatNum($al_hours, 3),
			($wd_hours <= 0) ? "" : FormatNum($wd_hours, 3));
		 else
	            printf("<td class=\"$class\"><input type=\"text\" name=\"$name\" value=\"%s\" size=6 maxlength=6></td>\n",
			($wd_hours <= 0) ? "" : FormatNum($wd_hours, 3));
	      }
	      else if ($pplan)
	         printf("<td class=\"selnum\">%s<br>%s</td>\n",
	            FormatNum($al_hours, 3), FormatNum($wd_hours, 3));
	      else
	         printf("<td class=\"selnum\"><br>%s</td>\n", ($wd_hours <= 0) ? "" : FormatNum($wd_hours, 3));

	      if ($pmlight)
	         $akt = IncDate($periode, $akt);
	      else
		 $akt += 86400;

	      $sumi += $wd_hours;
	      $sumiarr[$z] += $wd_hours;
	      $z++;
	   }

	   if ($pmlight) {
	      printf("<td class=\"selakt\" align=\"right\"><b>%s</b></td>\n", FormatNum($sump, 3));

	      if ($sumi > $sump && $pr_status == 0)
	         printf("<td class=\"five\" align=\"right\"><br><b>%s</b></td>\n", FormatNum($sumi, 3));
	      else
	         printf("<td class=\"selakt\" align=\"right\"><br><b>%s</b></td>\n", FormatNum($sumi, 3));
	   } else
	      printf("<td class=\"selakt\" align=\"right\"><b>%s</b></td>\n", FormatNum($sumi, 3));

	   printf("<td class=\"selnum\"><b>%s</b></td>\n", FormatNum($plan, 3));

	   if (!$ist_erledigung && $ist > $plan && $pr_status == 0) {
	      if ($pmlight)
	         printf("<td class=\"five\" align=\"right\" valign=\"top\"><br><b>%s</b></td>\n", FormatNum($ist, 3));
	      else
	         printf("<td class=\"five\" align=\"right\" valign=\"top\"><b>%s</b></td>\n", FormatNum($ist, 3));
	   } else {
	      if ($pmlight)
	         printf("<td class=\"selnum\"><b><br>%s</b></td>\n", FormatNum($ist, 3));
	      else
	         printf("<td class=\"selnum\"><b>%s</b></td>\n", FormatNum($ist, 3));
	   }

	   $sumallp += $plan;
	   $sumalli += $ist;

	   if ($ist_erledigung) {
	      if ($proz > 100.0 && $pr_status == 0)
	         printf ("<td class=\"five\" align=\"right\">%s%%</td></tr>\n", FormatNum($proz, 1));
	      else
	         printf ("<td class=\"selnum\">%s%%</td></tr>\n", FormatNum($proz, 1));
	   }

	   $r++;
	}

	return true;
}

# Folgende Funktion verwaltet und visualisiert die Auswahlmoeglichkeiten
# fuer die IST-Erfassung. Fuer die Darstellung und Eingabefelder der
# eigentlichen Isterfassung fuer einen Mitarbeiter, siehe ShowMitarbeiter()
#
function ManageTime() {
	global $phase;
	global $pmlight;
	global $rstufe;
	global $unum;
	global $einheit;
	global $leneinheit;
	global $periode;
	global $ist_erledigung;
	global $ist_future;
	global $menu;
	global $headline;
	global $sumparr;
	global $sumiarr;
	global $sumallp;
	global $sumalli;
	global $dtformatshort;

	$db = OpenDB();
	$nav = "menu=$menu&headline=$headline&project='+";
	$nav .= "document.ist.project.value+'&mitarbeiter='+";
	$nav .= "document.ist.mitarbeiter.value+'&von='+";
	$nav .= "document.ist.von.value+'&bis='+document.ist.bis.value+'";

	# Per default waehlen wir alle Projekte (eines Mitarbeiters) aus,
	# alle Mitarbeiter die diesem Projekt zugeordnet sind, sofern es sich
	# um den Projektleiter handelt und die im Zeitraum der aktuellen
	# Periode sind, aus.
	#
	if ($rstufe == 2 || $rstufe == 3) {
	   $query = "select distinct on (pr_num) pr_num, pr_name, pr_pl, ";
	   $query .= "mi_nname, mi_vname, pl_num, pl_lfd, pl_status ";
	   $query .= "from project, plan, mitarbeiter, task, allocation ";
	   $query .= "where pl_prnum = pr_num and mi_num = pr_pl ";
	   $query .= "and pl_status in (0, 2, 3, 4, 5) and pl_num = ta_plnum and ";
	   $query .= "ta_num = al_task and ";

	   if ($rstufe == 2)
	      $query .= "(al_ressource = $unum or pr_pl = $unum or pr_status = 1) ";
	   else
	      $query .= "(al_ressource = $unum or pr_status = 1)";
	} else {
	   $query = "select distinct on (pr_num) pr_num, pr_name, pr_pl, mi_nname, mi_vname, ";
	   $query .= "pl_num, pl_lfd, pl_status ";
	   $query .= "from project, plan, mitarbeiter where ";
	   $query .= "pl_prnum = pr_num and mi_num = pr_pl and ";
	   $query .= "pl_status in (0, 2, 3, 4, 5) ";
	}

	$query .= "order by pr_num, pl_lfd desc";
//	$query .= "order by pr_num, pl_lfd desc, pl_status desc";

	if (!($result = QueryDB($db, $query))) {
	   closeDB($db);
	   return;
	}

	$rows = numrowsDB($result);

	if ($rows <= 0) {
	   Error("Es wurden keine bebuchbaren Projekte gefunden!");
	   closeDB($db);
	   return;
	}

	# Auswahlfelder fuer die IST-Buchungen anzeigen
	$project = $_REQUEST['project'];
	$mitarbeiter = $_REQUEST['mitarbeiter'];
	$von = $_REQUEST['von'];
	$bis = $_REQUEST['bis'];

	if (!isset($mitarbeiter))
	   $mitarbeiter = $unum;

	if (isset($von) && isset($bis)) {
	   $dxa = explode(".", $von);
	   $dxb = explode(".", $bis);

	   if ($pmlight) {
	      $dta = gmmktime(0, 0, 0, $dxa[0], 1, $dxa[1]);
	      $dtb = gmmktime(0, 0, 0, $dxb[0], 1, $dxb[1]);
	   } else {
	      $dta = gmmktime(0, 0, 0, $dxa[1], $dxa[0], $dxa[2]);
	      $dtb = gmmktime(0, 0, 0, $dxb[1], $dxb[0], $dxb[2]);
	   }

	   if ($dta > $dtb) {
	      Error("Das \"Datum von\" darf nicht gr&ouml;&szlig;er als das \"Datum bis\" sein!");
	      unset($von);
	      unset($bis);
	   }

	   unset($dxa);
	   unset($dxb);
	   unset($dta);
	   unset($dtb);
	}


	echo "<form name=\"ist\" action=\"ist_alt.php\" method=\"post\" onChange=\"javascript:KeyEvent(this)\">\n";
	echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
	echo "<input type=\"hidden\" name=\"headline\" value=\"$headline\">\n";
	echo "<input type=\"hidden\" name=\"func\" value=\"save\">\n";
	echo "<table class=\"input\">\n<tr>\n";
	# Projekte anzeigen
	echo "<td>Projekte</td>\n";
	echo "<td><select name=\"project\" ";
	echo "onChange=\"javascript:MoveTo('ist_alt.php','$nav')\">\n";
	echo "<option value=\"0\">Alle</option>\n";
	$r = 0;
	$flag = false;

	while ($r < $rows) {
	   $data = fetchDB($result, $r);
	   $pr_num = $data[0];
	   $pr_name = $data[1];
	   $plnum = $data[5];
	   $pl_lfd = $data[6];
	   $pl_status = $data[7];

	   if ($pl_status == 6) {
	      $r++;
	      continue;
	   }

	   if ($pr_num == $project) {
	      echo "<option value=\"$pr_num\" selected>$pr_num $pr_name</option>\n";
	      $pl_selnum = $plnum;
	      $flag = true;
	   } else
	      echo "<option value=\"$pr_num\">$pr_num $pr_name</option>\n";

	   $r++;
	}

	echo "</select></td></tr>\n";

	if (!$flag)
	   $project = 0;

	# optional Mitarbeiter anzeigen
	if ($rstufe == 1 || $rstufe == 2) {
	   echo "<tr><td>Mitarbeiter</td><td><select name=\"mitarbeiter\" ";
	   echo "onChange=\"javascript:MoveTo('ist_alt.php','$nav')\">\n";
	   echo "<option value=\"0\">Alle</option>\n";

	   $r = 0;

	   while ($r < $rows) {
	      $data = fetchDB($result, $r);
	      $pl_num[$r] = $data[5];
	      $pl_status[$r] = $data[7];
	      $r++;
	   }

	   if (isset($pl_selnum) && $pl_selnum > 0) {
	      $query = "select pl_status from plan where pl_num = $pl_selnum";

	      if (!($res = QueryDB($db, $query))) {
	         closeDB($db);
	         return;
	      }

	      $data = fetchDB($res, 0);
	      $pstat = $data[0];
	   } else
	      $pstat = -1;

	   if ($pstat != 0) {
	      $query = "select distinct al_ressource, mi_nname,";
	      $query .= "mi_vname from ";
	      $query .= "allocation, task, mitarbeiter where ";
	      $query .= "ta_num = al_task and mi_num = al_ressource and ";
	      $query .= "ta_plnum in (";

	      if (isset($pl_selnum) && $pl_selnum > 0) {
	         $query .= "$pl_selnum";
	      } else {
	         $r = 0;
	         $flag = false;

	         while ($r < $rows) {
	            if ($pl_status[$r] == 6) {
	               $r++;
	               continue;
	            }

	            if ($flag)
	               $query .= "," . $pl_num[$r];
	            else
	               $query .= $pl_num[$r];

		    $flag = true;
	            $r++;
	         }
	      }

	      $query .= ") order by mi_nname, mi_vname";
	   } else {
	      $query = "select mi_num, mi_nname, mi_vname from mitarbeiter ";
	      $query .= "where mi_plan = true order by mi_nname, mi_vname";
	   }

	   if (!($resmi = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $nr = numrowsDB($resmi);
	   $r = 0;
	   $flag = false;

	   while ($r < $nr) {
	      $data = fetchDB($resmi, $r);
	      $al_ressource = $data[0];
	      $mi_nname = $data[1];
	      $mi_vname = $data[2];

	      if ($al_ressource == $mitarbeiter) {
	         echo "<option value=\"$al_ressource\" selected>$mi_nname $mi_vname</option>\n";
	         $flag = true;
	      } else
	         echo "<option value=\"$al_ressource\">$mi_nname $mi_vname</option>\n";

	      $r++;
	   }

	   echo "</select></td></tr>\n";

	   if (!$flag)
	      $mitarbeiter = 0;
	}

	# Zeitraum von anzeigen
	$vd = GetVisualDate($dtformatshort, $pmlight);
	echo "<tr><td>Zeitraum von ($vd):</td>\n";

	if (isset($von) && strlen($von) >= 4) {
	   $sd = explode(GetDateSep(), $von);

	   if ($pmlight)
	      $dat = gmmktime(0, 0, 0, $sd[0], 1, $sd[1]);
	   else
	      $dat = gmmktime(0, 0, 0, $sd[1], $sd[0], $sd[2]);

	   unset($sd);
	} else {
	   if ($pmlight) {
	      $dat = time();
	      $mon = gmdate("n", $dat);
	      $year = gmdate("Y", $dat);

	      if ($mon <= 3) {
		 $mon = 12 - (3 - $mon) + 1;

		 if ($mon == 13)
		    $mon = 1;
		 else
		    $year--;
	      } else
		 $mon -= 2;

	      $dat = gmmktime(0, 0, 0, $mon, 1, $year);
	   } else
	      $dat = time() - 86400 * 6;
	}

	$mon = gmdate("n", $dat);
	$year = gmdate("Y", $dat);
	$day = gmdate("j", $dat);

	$dvon = gmmktime(0, 0, 0, $mon, $day, $year);
//	$anz = ($pmlight)? 3 : 7;
	echo "<td><table border=0 cellpadding=0><tr><td><input type=\"text\" name=\"von\" value=\"";

	if ($pmlight) {
	   $vd = GetShortDate($dvon);
	   echo "$vd\" size=7 maxlength=7 onBlur=\"javascript:CheckDate(this,1)\">";
	} else {
	   $vd = gmdate($dtformatshort, $dvon);
	   echo "$vd\" size=10 maxlength=10 onBlur=\"javascript:CheckDate(this,0)\">";
	}

	# Zeitraum bis anzeigen
	if (isset($bis) && strlen($bis) >= 4) {
	   $sd = explode(GetDateSep(), $bis);

	   if ($pmlight)
	      $dat = gmmktime(0, 0, 0, $sd[0], daysinmonth($sd[0], $sd[1]), $sd[1]);
	   else
	      $dat = gmmktime(0, 0, 0, $sd[1], $sd[0], $sd[2]);

	   unset($sd);
	} else {
	   $dat = time();

	   if ($pmlight) {
	      $mon = gmdate("n", $dat);
	      $year = gmdate("Y", $dat);
	      $aday = gmdate("j", $dat);
	      $day = daysinmonth($mon, $year);

	      if (($day / 2) < $aday) {
	         $mon++;

	         if ($mon > 12) {
	            $year++;
	            $mon = 1;
		 }

		 $day = daysinmonth($mon, $year);
	      }

	      $dat = gmmktime(0, 0, 0, $mon, $day, $year);
	   }
	}

	$mon = gmdate("n", $dat);
	$year = gmdate("Y", $dat);
	$day = gmdate("j", $dat);
	$dbis = gmmktime(0, 0, 0, $mon, $day, $year);
	echo " Bis: <input type=\"text\" name=\"bis\" value=\"";

	if ($pmlight) {
	   $vd = GetShortDate($dbis);
	   echo "$vd\" size=7 maxlength=7 onBlur=\"javascript:CheckDate(this,1)\"></td>\n";
	} else {
	   $vd = gmdate($dtformatshort, $dbis);
	   echo "$vd\" size=10 maxlength=10 onBlur=\"javascript:CheckDate(this,0)\"></td>\n";
	}

	echo "<td>";
	ButtonLink("Zeitraum setzen", "ist_alt.php","$nav");
	echo "</td></tr></table></td></tr></table>\n";
	# Button zum Senden / Rueckgaengig machen
	echo "<br><table class=\"indent\"><tr><td>\n";
	ButtonSubmit("Speichern", "ist");
	echo "</td></tr></table>\n";
	# Aufbau der Tabelle zur IST-Erfassung
	#
	# Tabellenkopf
	echo "<table class=\"sel\"><tr>\n";

	if ($pmlight)
	   echo "<td class=\"sel\" colspan=3></td>\n";
	else
	   echo "<td class=\"sel\" colspan=2></td>\n";

	# Ermitteln des gewuenschten Zeitraums
	$anz = GetAnzEntries($dvon, $dbis);

	if ($anz > 1)
	   echo "<th class=\"sel\" colspan=$anz>";
	else
	   echo "<th class=\"sel\">";

	if (!$pmlight)
	   echo "Stunden pro PT</th>\n";
	else
	   echo "Perioden</th>\n";

	if ($pmlight)
	   echo "<th class=\"sel\" colspan=2>Summe&nbsp;Zeile</th>\n";
	else
	   echo "<th class=\"sel\">Summe&nbsp;Zeile</th>\n";

	if ($ist_erledigung)
	   echo "<th class=\"sel\" colspan=3>Summe&nbsp;(PT)/Task</th></tr>\n";
	else
	   echo "<th class=\"sel\" colspan=2>Summe&nbsp;(PT)/Task</th></tr>\n";

	echo "<tr><th class=\"sel\">Projekt&nbsp;/&nbsp;Task</th>\n";

	if ($rstufe != 3 && isset($mitarbeiter) && $mitarbeiter == 0)
	   echo "<th class=\"sel\">PL&nbsp;/&nbsp;Mitarbeiter</th>\n";
	else
	   echo "<th class=\"sel\">Projektleiter</th>\n";

	if ($pmlight)
	   echo "<th class=\"sel\"></th>\n";

	$r = 0;
	$dt = $dvon;

	while ($r < $anz) {
	   if ($pmlight) {
	      $datum = GetShortDate($dt);
	      $dt = IncDate($periode, $dt);
	   } else {
	      $datum = gmdate($dtformatshort, $dt);
	      $dt += 86400;
	   }

	   echo "<th class=\"sel\">$datum</th>\n";
	   $r++;
	}

	if ($pmlight)
	   echo "<th class=\"sel\">Plan</th>\n";

	echo "<th class=\"sel\">IST</th>\n";
	echo "<th class=\"sel\">Plan</th>\n";
	echo "<th class=\"sel\">IST</th>\n";

	if ($ist_erledigung)
	   echo "<th class=\"sel\">Erl.</th>\n";

	echo "</tr>\n";
	# Tabelle mit Inhalten fuellen...
	$r = 0;

	while ($r < $rows) {
	   $data = fetchDB($result, $r);
	   $pr_num = $data[0];
	   $pr_name = $data[1];
	   $pr_pl = $data[2];
	   $mi_nname = $data[3];
	   $mi_vname = $data[4];
	   $pl_num = $data[5];
	   $pl_lfd = $data[6];
	   $pl_status = $data[7];

	   # Projekte die einen Plan mit Status = 6 (Abgeschlossen) haben,
	   # ignorieren! Sollten nicht vorkommen!
	   $query = "select count(*) from plan where pl_prnum = $pr_num and pl_status = 6";

	   if (!($resco = QueryDB($db, $query))) {
	      closeDB($db);
	      return;
	   }

	   $data = fetchDB($resco, 0);
	   $menge = $data[0];

	   if ($pl_status == 6 || $menge > 0) {
	      $r++;
	      continue;
	   }

	   # Wenn ein bestimmtes Projekt gewaehlt wurde, nur dieses anzeigen.
	   if (isset($project) && $project > 0 && $pr_num != $project) {
	      $r++;
	      continue;
	   }

	   # Pruefen, ob es fuer das aktuelle Projekt auch Tasks zum Anzeigen
	   # gibt. Falls nicht, untersuchen wir das naechste Projekt.
	   if ($pl_status > 0) {
	      $query = "select count(*) from task, plan, allocation where ";
	      $query .= "pl_num = ta_plnum and al_task = ta_num and ";

	      if (!$ist_future) {
	         $query .= "ta_start <= $dbis and ";
	      } else {
	         $query .= "(ta_start between $dvon and $dbis or ";
	         $query .= "ta_start+ta_duration*86400 between $dvon and $dbis or ";
	         $query .= "$dvon between ta_start and ta_start+ta_duration*86400 or ";
	         $query .= "$dbis between ta_start and ta_start+ta_duration*86400) and ";
	      }

//	      $query .= "ta_plnum = $pl_num and ";
	      $query .= "pl_prnum = $pr_num ";

	      if ($rstufe == 3)
	         $query .= " and al_ressource = $unum";
	      else if (isset($mitarbeiter) && $mitarbeiter > 0)
	         $query .= " and al_ressource = $mitarbeiter";

	      if (!($resta = QueryDB($db, $query))) {
	         closeDB($db);
	         return;
	      }

	      $data = fetchDB($resta, 0);

	      if ($data[0] == 0) {
	         $r++;
	         continue;
	      }
	   }

	   echo "<tr><td class=\"selakt\">$pr_num&nbsp;$pr_name</td>\n";

	   if ($pl_status > 0)
	      echo "<td class=\"selakt\">$mi_nname&nbsp;$mi_vname</td>\n";
	   else
	      echo "<td class=\"selakt\">&nbsp;</td>\n";

	   echo "<td class=\"selakt\"></td>\n";
	   echo "<td class=\"selakt\" colspan=$anz></td>\n";
	   echo "<td class=\"selakt\" colspan=5></td></tr>\n";
	   # Abhaengig von der Rolle des angemeldeten Users, werden alle
	   # Tasks pro Mitarbeiter angezeigt.
	   #
	   if ($rstufe == 3) { 		// Mitarbeiter
	      if (!ShowMitarbeiter($db, $unum, $anz, $dvon, $dbis, $pr_num, $pl_num, false)) {
	         closeDB($db);
	         return;
	      }
	   } else if (isset($mitarbeiter) && $mitarbeiter > 0) {
	      if (!ShowMitarbeiter($db, $mitarbeiter, $anz, $dvon, $dbis, $pr_num, $pl_num, false)) {
		 closeDB($db);
		 return;
	      }
	   } else {
	      if ($pl_status > 0) {
	         $query = "select distinct al_ressource, mi_nname, mi_vname, pl_lfd ";
		 $query .= "from allocation, task, plan, mitarbeiter where ";
//		 $query .= "ta_num = al_task and ta_plnum = $pl_num and ";
		 $query .= "ta_num = al_task and pl_prnum = $pr_num and ";
		 $query .= "mi_num = al_ressource and pl_num = ta_plnum and ";

		 if (!$ist_future) {
		    $query .= "ta_start <= $dbis ";
		 } else {
		    $query .= "(ta_start between $dvon and $dbis or ";
		    $query .= "ta_start+ta_duration*86400 between $dvon and $dbis or ";
		    $query .= "$dvon between ta_start and ta_start+ta_duration*86400 or ";
		    $query .= "$dbis between ta_start and ta_start+ta_duration*86400) ";
		 }

		 $query .= "order by pl_lfd desc, mi_nname, mi_vname";
	      } else {
		 $query = "select mi_num, mi_nname, mi_vname from mitarbeiter ";
		 $query .= "where mi_plan = true order by mi_nname, mi_vname";
	      }

	      if (!($resal = QueryDB($db, $query))) {
		 closeDB($db);
		 return;
	      }

	      $cnt = numrowsDB($resal);
	      $z = 0;

	      while ($z < $cnt) {
	         $data = fetchDB($resal, $z);
	         $al_ressource = $data[0];

	         if (!ShowMitarbeiter($db, $al_ressource, $anz, $dvon, $dbis, $pr_num, $pl_num)) {
	            closeDB($db);
	            return;
	         }

	         $z++;
	      }
	   }

	   $r++;
	}

	# Endsummen schreiben
	if ($pmlight) {
	   echo "<tr><td class=\"selnum\" colspan=3>Summen Plan:<br>Summen IST:</td>\n";
	   $z = 0;
	   $sumptot = 0.0;
	   $sumitot = 0.0;

	   while ($z < $anz) {
	      printf("<td class=\"selltgray\" align=\"right\"><b>%s<br>%s</b></td>", FormatNum($sumparr[$z], 3),
	      		FormatNum($sumiarr[$z], 3));
	      $sumptot += $sumparr[$z];
	      $sumitot += $sumiarr[$z];
	      $z++;
	   }

	   printf("<td class=\"selgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumptot, 3));
	   printf("<td class=\"selgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumitot, 3));
	   echo "<td class=\"sel\" colspan=2>&nbsp;</td>";
	} else {
	   echo "<tr><td class=\"selnum\" colspan=2>Summen:</td>\n";
	   $z = 0;
	   $sumitot = 0.0;

	   while ($z < $anz) {
	      printf("<td class=\"selltgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumiarr[$z], 3));
	      $sumitot += $sumiarr[$z];
	      $z++;
	   }

	   printf("<td class=\"selgray\" align=\"right\"><b>%s</b></td>", FormatNum($sumitot, 3));
	   printf("<td class=\"selnum\"><b>%s</b></td>", FormatNum($sumallp, 3));
	   printf("<td class=\"selnum\"><b>%s</b></td>", FormatNum($sumalli, 3));
	}

	if (!$pmlight && $ist_erledigung) {
	   if ($sumallp == 0)
	      printf("<td class=\"selnum\"><b>%s%%</b></td>", FormatNum(0.0, 1));
	   else
	      printf("<td class=\"selnum\"><b>%s%%</b></td>", FormatNum($sumalli / $sumallp * 100.0, 1));
	} else if ($ist_erledigung)
	   echo "<td class=\"sel\">&nbsp;</td>\n";

	echo "</tr>\n";
	echo "</table></form>\n";
	closeDB($db);
}

# Folgende Funktion prueft die uebergebenen Parameter und speichert die
# neuen oder veraenderten IST-Zeiten in die Datenbank.
#
function SaveIST() {
	global $unum;
	global $pmlight;
	global $leneinheit;

	$kill = false;
	$db = OpenDB();

	if (!($result = QueryDB($db, "select co_wdone from counter"))) {
	   closeDB($db);
	   return false;
	}

	$data = fetchDB($result, 0);
	$co_wdone = $data[0];

	foreach ($_REQUEST as $key => $hours) {
	   $fr = substr($key, 0, 3);

	   if ($fr != "dt_")
	      continue;

	   $pars = explode("_", $key);
	   $minum = $pars[1];
	   $pr_num = $pars[2];
	   $wd_task = $pars[3];
	   $dt = $pars[4];

	   if (!isset($hours) || $hours == "")
	      $hours = 0.0;
	   else
	      $hours = str_replace(",", ".", $hours);

	   if ($minum <= 0 || $pr_num <= 0 || $wd_task <= 0 || $dt <= 0) {
	      Error("Interner Fehler: Fehlender Parameter!");
	      closeDB($db);
	      return false;
	   }

	   $query = "select ta_phase from task where ta_num = $wd_task";

	   if (!($resta = QueryDB($db, $query))) {
	      closeDB($db);
	      return false;
	   }

	   $data = fetchDB($resta, 0);
	   $ta_phase = $data[0];

	   if (!isset($ta_phase))	// Task eines Kontos?
	      $ta_phase = 0;

	   $query = "select wd_num from wdone where ";
	   $query .= "wd_minum = $minum and wd_task = $wd_task and ";

	   if ($pmlight) {
	      $day = 1;
	      $mon = gmdate("n", $dt);
	      $year = gmdate("Y", $dt);
	      $fdate = gmmktime(0, 0, 0, $mon, 1, $year);
	      $ldate = gmmktime(0, 0, 0, $mon, daysinmonth($mon, $year), $year);
	      $query .= "wd_datum between $fdate and $ldate ";
	   } else {
	      $query .= "wd_datum = $dt ";
	   }

	   if (!($reswd = QueryDB($db, $query))) {
	      closeDB($db);
	      return false;
	   }

	   $anz = numrowsDB($reswd);

	   if ($pmlight) {
	      if (!QueryDB($db, "begin")) {
		 closeDB($db);
		 return false;
	      }

	      if ($anz > 0) {
		 $query = "delete from wdone where wd_minum = $minum and ";
		 $query .= "wd_task = $wd_task and ";
		 $query .= "wd_datum between $fdate and $ldate";

		 if (!QueryDB($db, $query)) {
		    QueryDB($db, "rollback");
		    closeDB($db);
		    return false;
		 }
	      }

	      $j = 0;
	      $h = $hours * $leneinheit;
	      $days = daysinmonth($mon, $year);

	      while ($j < $h) {
		 $datum = gmmktime(0, 0, 0, $mon, $day, $year);
		 $co_wdone++;

		 if (($h - $j) < $leneinheit)
		    $ho = $h - $j;
		 else
		    $ho = $leneinheit;

		 $query = "insert into wdone (wd_num, wd_prnum, wd_minum, ";
		 $query .= "wd_datum, wd_hours, wd_task, wd_rk, wd_phase, ";
		 $query .= "wd_edit, wd_verr) values ($co_wdone, $pr_num, ";
		 $query .= "$minum, $datum, $ho, $wd_task, 0, $ta_phase,";
		 $query .= "true, true)";

		 if (!QueryDB($db, $query)) {
		    QueryDB($db, "rollback");
		    closeDB($db);
		    return false;
		 }

		 $j += $leneinheit;
		 $day++;

		 if ($day > $days)
		    $day = 1;
	      }

	      if (!QueryDB($db, "update counter set co_wdone = $co_wdone")) {
		 QueryDB($db, "rollback");
		 closeDB($db);
		 return false;
	      }

	      QueryDB($db, "commit");
	   } else {
	      if ($anz > 0) {
	         $data = fetchDB($reswd, 0);
	         $wd_num = $data[0];

		 if ($hours <= 0)
		    $query = "delete from wdone where wd_num = $wd_num";
		 else
		    $query = "update wdone set wd_hours = $hours where wd_num = $wd_num";

	         if (!QueryDB($db, $query)) {
	            QueryDB($db, "rollback");
	            closeDB($db);
	            return false;
	         }

	         QueryDB($db, "commit");
	      } else if ($hours > 0) {
	         $co_wdone++;
	         $query = "insert into wdone (wd_num, wd_prnum, wd_minum, ";
	         $query .= "wd_datum, wd_hours, wd_task, wd_rk, wd_phase, ";
	         $query .= "wd_edit, wd_verr) values ($co_wdone, $pr_num, ";
	         $query .= "$minum, $dt, $hours, $wd_task, 0, $ta_phase,";
	         $query .= "true, true)";

	         if (!QueryDB($db, $query)) {
		    QueryDB($db, "rollback");
		    closeDB($db);
		    return false;
	         }

	         if (!QueryDB($db, "update counter set co_wdone = $co_wdone")) {
		    QueryDB($db, "rollback");
		    closeDB($db);
		    return false;
	         }

	         QueryDB($db, "commit");
	      }
	   }
	}

	Journal(302, "ist_alt.php: SaveIST: Projekt: $pr_num, Mitarbeiter: $minum", $db);
	closeDB($db);
	return true;
}

if ($func == "save")
   SaveIST();

ManageTime();

require_once('footer.inc');
?>
